<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:php="http://php.net/xsl" exclude-result-prefixes="php">
	<xsl:output method="xml" omit-xml-declaration="yes" doctype-public="" indent="yes" doctype-system=""/>
	<xsl:strip-space elements="*" />
    	<!--xsl:include href="config.xsl" /-->
	<xsl:include href="../../xsl/dita2xhtml.xsl" />
	<xsl:param name="mapRoot" />
	<xsl:param name="thisPath" />
    	<xsl:param name="serverRoot" />
    	<xsl:param name="serviceRoot" />
	<xsl:param name="id" />
	<xsl:param name="navtitle" />
	<xsl:param name="cmis_id" />
	<xsl:param name="audience" />
	<!--xsl:template match="*">
		<xsl:apply-templates />
	</xsl:template-->
	
	<xsl:template match="/">
		<div>    
        		<xsl:choose>
				<xsl:when test="//*[contains(@class, ' topic/topic ') and @id = $id]">
				<h1>
                			<xsl:call-template name="getTitle">
                    				<xsl:with-param name="navtitle" select="$navtitle" />
                			</xsl:call-template>
                		</h1>
				<xsl:apply-templates select="//*[contains(@class, ' topic/topic ') and @id = $id]/*[contains(@class, ' topic/shortdesc ')]" />
				<xsl:apply-templates select="//*[contains(@class, ' topic/topic ') and @id = $id]/*[contains(@class, ' topic/body ')]" />
				<xsl:apply-templates select="//*[contains(@class, ' topic/topic ') and @id = $id]/*[contains(@class, ' topic/topic ')]" />
				</xsl:when>
				<xsl:otherwise>
					<xsl:apply-templates  />
				</xsl:otherwise>
			</xsl:choose>
		</div>
    	</xsl:template>

	<xsl:template match="*[@audience]" priority="2">
		<xsl:param name="nesting_level" select="boolean(false)"/>
		<xsl:if test="php:function('hasAudience',string($audience), string(@audience)) or $audience = ''">
			<xsl:choose>
				<xsl:when test="contains(@class, ' map/topicref ')">
					<xsl:call-template  name="topicref" />
				</xsl:when>
				<xsl:otherwise>
					<xsl:apply-templates>
						<xsl:with-param name="nesting_level" select="$nesting_level" />
					</xsl:apply-templates>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:if>
	</xsl:template>


	<xsl:template match="dita">
		<xsl:apply-templates />
	</xsl:template>
	
	<xsl:template match="*[contains(@class, ' topic/topic ')]">
		<xsl:param name="title" />
		<xsl:param name="nesting_level" select="boolean(false)"/>
		<xsl:variable name="bodyContent" select="string-length(normalize-space(translate(*[contains(@class, ' topic/body ')],' ','')))" />

		<!--xsl:if test="$bodyContent &gt; 0"-->
		<div style="padding-left:2em">
		<xsl:variable name="depth">
		<xsl:choose>
			<xsl:when test="$nesting_level != boolean(false)">
				<xsl:value-of select="$nesting_level"  />
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="count(ancestor::*[ contains(@class, ' topic/topic ') or contains(@class, ' map/topichead ') or contains(@class, ' map/topicref ') or contains(@class, ' map/map ')])+1" />
			</xsl:otherwise>
		</xsl:choose>
		</xsl:variable>
		<xsl:element name="h{$depth}">
			<xsl:choose>
				<xsl:when test="$title and $title != ''">
					<xsl:value-of select="$title" />
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="*[contains(@class, ' topic/title ')]" />
				</xsl:otherwise>
			</xsl:choose>
		</xsl:element>
		<xsl:apply-templates select="*[contains(@class, ' topic/shortdesc ')]" />
		<xsl:apply-templates select="*[contains(@class, ' topic/body ')]"/>
		<xsl:apply-templates select="*[contains(@class, ' topic/topic ')]">
			<xsl:with-param name="nesting_level" select="$depth + 1" />
		</xsl:apply-templates>
		</div>
		<!--/xsl:if-->

    </xsl:template>

	<xsl:template match="*[contains(@class, ' map/topicmeta ')]">
    	</xsl:template>

	<xsl:template match="*[contains(@class, ' topic/shortdesc ')]">
		<div><xsl:apply-templates /></div>
    	</xsl:template>
	
	<xsl:template match="*[contains(@class, ' topic/section ')]">
		<xsl:apply-templates />
    	</xsl:template>

	<xsl:template match="*[contains(@class, ' topic/body ')]">
		<xsl:apply-templates />
    	</xsl:template>

	<xsl:template match="*[contains(@class, ' topic/p ')]">
		<div><xsl:apply-templates /></div>
    	</xsl:template>
	
	<xsl:template match="*[contains(@class, ' topic/title ')]">
		<xsl:param name="nesting_level" select="'1'" />
		<xsl:variable name="depth">
		<xsl:choose>
			<xsl:when test="$nesting_level != boolean(false)">
				<xsl:value-of select="$nesting_level"  />
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="count(ancestor::*[ contains(@class, ' topic/topic ') or contains(@class, ' map/topichead ') or contains(@class, ' map/topicref ') or contains(@class, ' map/map ')])+1" />
			</xsl:otherwise>
		</xsl:choose>
		</xsl:variable>
		<xsl:element name="h{$depth}">
		<xsl:value-of select="." />
		</xsl:element>
    	</xsl:template>
	
	
	<xsl:template match="processing-instruction()">
		<!--:)a-->
    	</xsl:template>

	<!-- Map Processing -->
	<xsl:template match="*[contains(@class, ' map/map ')]">
	<div>
		<h1><xsl:call-template name="getTitle" /></h1>
		<xsl:apply-templates />
	</div>
    </xsl:template>


	<xsl:template name="getTitle">
    <xsl:param name="navtitle" />
	<xsl:variable name="fullPath" select="concat($mapRoot, @href)" />
	<xsl:variable name="path">
		<xsl:choose>
			<xsl:when test="$thisPath">
				<xsl:value-of select="$thisPath" />
			</xsl:when>
			
			<xsl:when test="contains(@href, '#')">
				<xsl:value-of select="substring-before(@href, '#')" />
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="@href" />
			</xsl:otherwise>
		</xsl:choose>
	</xsl:variable>
	<xsl:variable name="topic_id">
		<xsl:choose>
			<xsl:when test="contains(@href, '#')">
				<xsl:value-of select="substring-after(@href, '#')" />
			</xsl:when>
			<xsl:when test="$id">
				<xsl:value-of select="$id" />
			</xsl:when>
		</xsl:choose>
	</xsl:variable>
	<xsl:variable name="retTitle">
	<xsl:choose>
		<xsl:when test="@navtitle">
			<xsl:value-of select="@navtitle" />
		</xsl:when>
        <xsl:when test="$navtitle">
            <xsl:value-of select="$navtitle" />
        </xsl:when>
	<xsl:when test="@title">
            <xsl:value-of select="@title" />
	</xsl:when>
        <xsl:when test="$path">
		<!-- Gives error-->
        	<!--xsl:value-of select="php:function('getDocument',string($path))//*[@id = $topic_id]/title" /-->
	</xsl:when>
        <xsl:when test="//*[contains(@class, ' topic/topic ')][@id = $id]/*[contains(@class, ' topic/title ')]">
            <xsl:value-of select="//*[contains(@class, ' topic/topic ')][@id = $id]/*[contains(@class, ' topic/title ')]" />
	</xsl:when>
        <xsl:when test="//*[contains(@class, ' topic/topic ')][1]/*[contains(@class, ' topic/title ')]">
            <xsl:value-of select="//*[contains(@class, ' topic/topic ')][1]/*[contains(@class, ' topic/title ')]" />
        </xsl:when>
        <xsl:otherwise>
			NO TITLE 
		</xsl:otherwise>
	</xsl:choose>
		</xsl:variable>
		<xsl:value-of select="$retTitle" />
	</xsl:template>
	
	<xsl:template match="*[contains(@class, ' map/topicref ')]" name="topicref">
		<xsl:variable name="fullPath" select="concat($mapRoot, @href)" />
		<xsl:variable name="path">
                <xsl:choose>
                        <xsl:when test="contains(@href, '#')">
                                <xsl:value-of select="substring-before(@href, '#')" />
                        </xsl:when>
                        <xsl:otherwise>
                                <xsl:value-of select="@href" />
                        </xsl:otherwise>
                </xsl:choose>
        	</xsl:variable>
		<xsl:variable name="topic_id" select="substring-after($fullPath, '#')" />
	
		<div style="padding-left:2em">
			<xsl:variable name="title">
				<xsl:call-template name="getTitle" />
			</xsl:variable>
			<xsl:if test="@href">
				<xsl:choose>
					<xsl:when test="contains(@href, '.ditamap')">
						<xsl:apply-templates select="php:function('CmisController::actionGetDocument',string($path))" />
						<!--xsl:value-of select="$fullPath" /-->
					</xsl:when>
                    			<xsl:when test="@search">      
                        			<xsl:apply-templates select="php:function('CmisController::actionGetDocument',string(@search))//*[@id = $topic_id]">
							<xsl:with-param name="title" select="$title" />
						</xsl:apply-templates>
                    			</xsl:when>
					<xsl:otherwise>
						<xsl:variable name="thisCmisId" select="php:function('CmisController::actionGetCmisId',string($fullPath))" />
						<xsl:choose>
							<xsl:when test="$topic_id != ''">
                        					<xsl:apply-templates select="php:function('CmisController::actionGetDocument',string($thisCmisId))//*[@id = $topic_id]">
									<xsl:with-param name="title" select="$title" />
									<xsl:with-param name="nesting_level" select="count(ancestor::*[ contains(@class, ' topic/topic ') or contains(@class, ' map/topichead ') or contains(@class, ' map/topicref ') or contains(@class, ' map/map ')]) + 1" />
								</xsl:apply-templates>
								
							</xsl:when>
							<xsl:otherwise>
                        					<xsl:apply-templates select="php:function('CmisController::actionGetDocument',string($thisCmisId))//*[contains(@class, ' topic/topic ') and position() = 1]">
									<xsl:with-param name="title" select="$title" />
									<xsl:with-param name="nesting_level" select="count(ancestor::*[ contains(@class, ' topic/topic ') or contains(@class, ' map/topichead ') or contains(@class, ' map/topicref ') or contains(@class, ' map/map ')]) + 1" />
								</xsl:apply-templates>
							</xsl:otherwise>
						</xsl:choose>
                        			<!--xsl:apply-templates select="php:function('getDocument',string($path))//*[@id = $topic_id]/*[contains(@class, ' topic/body ')]" /-->
					</xsl:otherwise>
				</xsl:choose>
			</xsl:if>
		<xsl:if test="contains(@class, ' mapgroup-d/topichead ')">
				<xsl:variable name="depth" select="count(ancestor::*[ contains(@class, ' topic/topic ') or contains(@class, ' map/topichead ') or contains(@class, ' map/topicref ') or contains(@class, ' map/map ')])+1" />
			<xsl:element name="h{$depth}">
			<xsl:value-of select="$title" />
			</xsl:element>
		</xsl:if>
			<xsl:apply-templates />
		</div>
	</xsl:template>

	<xsl:template match="*[contains(@class, ' topic/image ')]">  
        	<img src="{$serverRoot}?r=cmis/getImage&amp;id={php:function('CmisController::actionGetCmisId',string(@href))}" width="{@width}"  height="{@height}"/>
	</xsl:template>

	<xsl:template match="*[contains(@class , ' reference/properties ')]">
		<table style="border:solid 1px #999;border-collapse:collapse;width:100%">
			<xsl:apply-templates />
		</table>
	</xsl:template>
	<xsl:template match="*[contains(@class , ' reference/prophead ')]">
		<tr style="background:#999;color:#fff">
			<xsl:apply-templates />
		</tr>
	</xsl:template>
	<xsl:template match="*[contains(@class , ' reference/property ')]">
		<tr>
			<xsl:apply-templates />
		</tr>
	</xsl:template>

	<xsl:template match="*[contains(@class , ' reference/proptype ') or contains(@class , ' reference/propvalue ') or contains(@class , ' reference/propdesc ')]">
		<td style="border:solid 1px #999;border-collapse:collapse;padding:0.5em;vertical-align:top;">
			<xsl:apply-templates />
		</td>
	</xsl:template>

	<xsl:template match="*[contains(@class , ' reference/proptypehd ') or contains(@class , ' reference/propvaluehd ') or contains(@class , ' reference/propdeschd ')]">
		<th style="border:solid 1px #999;border-collapse:collapse; padding:0.5em;text-align:left;">
			<xsl:attribute name="width">
			<xsl:choose>
				<xsl:when test="contains(@class, ' reference/proptypehd ')">
					<xsl:value-of select="'20%'" />
				</xsl:when>
				<xsl:when test="contains(@class, ' reference/propvaluehd ' )">
					<xsl:value-of select="'20%'" />
				</xsl:when>
				<xsl:when test="contains(@class, ' reference/propdeschd ')">
					<xsl:value-of select="'60%'" />
				</xsl:when>
			</xsl:choose>
			</xsl:attribute>
			<xsl:value-of select="." />
		</th>
	</xsl:template>
	
	
</xsl:stylesheet>
